import cv2
import logging

# 配置日志
logging.basicConfig(level=logging.INFO)
LOG = logging.getLogger(__name__)

def capture_frame(rtsp_url):
    """从 RTSP 读取一帧图像"""
    cap = cv2.VideoCapture(rtsp_url)
    if not cap.isOpened():
        LOG.error("无法打开 RTSP 流")
        return None
    
    ret, frame = cap.read()
    cap.release()
    
    if not ret:
        LOG.error("无法捕获帧")
        return None
    
    return frame

def test_rtsp_stream(rtsp_url):
    """测试 RTSP 视频流"""
    LOG.info(f"正在测试 RTSP 流: {rtsp_url}")
    
    # 捕获一帧图像
    frame = capture_frame(rtsp_url)
    
    if frame is None:
        LOG.error("RTSP 流测试失败")
        return
    
    LOG.info("成功捕获一帧图像")
    
    # 显示捕获的图像
    cv2.imshow("Captured Frame", frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 示例：替换为你的 RTSP 流 URL
rtsp_url = "rtsp://116.56.143.13:25544/video"

# 测试 RTSP 流
test_rtsp_stream(rtsp_url)

